﻿using System;


using System.Text;
using IAM.Interface.Model.Interface;
using System.Xml;
using System.Web;
using System.Reflection;

namespace IAM.Interface.Model
{
    /// <summary>
    /// 权限项定义
    /// </summary>
    public class ACItem : AbstractConvertObject, IConvertXml
    {
        #region 构造函数
        public ACItem()
        { }
        public ACItem(ACItems acitems)
        {
            _acitems = acitems;
        }
        #endregion
        #region 属性
        private string _id = "";
        private string _display = "";
        private DimensionLevel _dimensionlevel = null;
        private ACItem _parent = null;
        private ACItems _acitems = null;
        private string _attr1 = "";
        private string _attr2 = "";
        private string _attr3 = "";
        private string _attr4 = "";
        private string _attr5 = "";
        private string _attr6 = "";
        private string _attr7 = "";
        private string _attr8 = "";
        /// <summary>
        /// 权限项标识
        /// </summary>
        public string ID
        {
            get
            {
                return _id;
            }
            set
            {
                _id = value;
            }
        }
        /// <summary>
        /// 权限项显示名
        /// </summary>
        public string Display
        {
            get
            {
                return _display;
            }
            set
            {
                _display = value;
            }
        }
        /// <summary>
        /// 如果该权限维度包含多层次时需要具有该属性，
        /// 该属性必须是已有的Level定义
        /// </summary>
        public DimensionLevel DimensionLevel
        {
            get
            {
                return _dimensionlevel;
            }
            set
            {
                _dimensionlevel = value;
            }
        }
        /// <summary>
        /// 如果该权限包含多层次
        /// 该属性为其父权限项
        /// </summary>
        public ACItem Parent
        {
            get
            {
                return _parent;
            }
            set
            {
                _parent = value;
            }
        }
        /// <summary>
        /// 上级的权限列表
        /// </summary>
        public ACItems Items
        {
            get
            {
                return _acitems;
            }
        }
        /// <summary>
        /// 扩展属性1
        /// </summary>
        public string Attr1
        {
            get
            {
                return _attr1;
            }
            set
            {
                _attr1 = value;
            }
        }
        /// <summary>
        /// 扩展属性2
        /// </summary>
        public string Attr2
        {
            get
            {
                return _attr2;
            }
            set
            {
                _attr2 = value;
            }

        }
        /// <summary>
        /// 扩展属性3
        /// </summary>
        public string Attr3
        {
            get
            {
                return _attr3;
            }
            set
            {
                _attr3 = value;
            }

        }
        /// <summary>
        /// 扩展属性4
        /// </summary>
        public string Attr4
        {
            get
            {
                return _attr4;
            }
            set
            {
                _attr4 = value;
            }

        }
        /// <summary>
        /// 扩展属性5
        /// </summary>
        public string Attr5
        {
            get
            {
                return _attr5;
            }
            set
            {
                _attr5 = value;
            }

        }
        /// <summary>
        /// 扩展属性6
        /// </summary>
        public string Attr6
        {
            get
            {
                return _attr6;
            }
            set
            {
                _attr6 = value;
            }

        }
        /// <summary>
        /// 扩展属性7
        /// </summary>
        public string Attr7
        {
            get
            {
                return _attr7;
            }
            set
            {
                _attr7 = value;
            }

        }
        /// <summary>
        /// 扩展属性8
        /// </summary>
        public string Attr8
        {
            get
            {
                return _attr8;
            }
            set
            {
                _attr8 = value;
            }

        }
        #endregion
        #region IConvertXml

        public string XMLBoday
        {
            get
            {
                StringBuilder _xml = new StringBuilder();
                _xml.Append("<ACItem");
                _xml.Append(" ID=\"" + IAM.Interface.Common.EnDeCoding.Encode(this.ID) + "\"");
                _xml.Append(" Display=\"" + IAM.Interface.Common.EnDeCoding.Encode(this.Display) + "\"");
                if (this.DimensionLevel != null)
                {
                    _xml.Append(" DimensionLevel=\"" + IAM.Interface.Common.EnDeCoding.Encode(this.DimensionLevel.ID) + "\"");
                }
                else
                {
                    _xml.Append(" DimensionLevel=\"" + "" + "\"");
                }
                if (this.Parent != null)
                {
                    _xml.Append(" Parent=\"" + IAM.Interface.Common.EnDeCoding.Encode(this.Parent.ID) + "\"");
                }
                else
                {
                    _xml.Append(" Parent=\"" + "" + "\"");
                }

                try
                {
                    _xml.Append(" Attr1=\"" + IAM.Interface.Common.EnDeCoding.Encode(this.Attr1) + "\"");
                    _xml.Append(" Attr2=\"" + IAM.Interface.Common.EnDeCoding.Encode(this.Attr2) + "\"");

                    _xml.Append(" Attr3=\"" + IAM.Interface.Common.EnDeCoding.Encode(this.Attr3) + "\"");
                    _xml.Append(" Attr4=\"" + IAM.Interface.Common.EnDeCoding.Encode(this.Attr4) + "\"");
                    _xml.Append(" Attr5=\"" + IAM.Interface.Common.EnDeCoding.Encode(this.Attr5) + "\"");
                    _xml.Append(" Attr6=\"" + IAM.Interface.Common.EnDeCoding.Encode(this.Attr6) + "\"");
                    _xml.Append(" Attr7=\"" + IAM.Interface.Common.EnDeCoding.Encode(this.Attr7) + "\"");
                    _xml.Append(" Attr8=\"" + IAM.Interface.Common.EnDeCoding.Encode(this.Attr8) + "\"");
                }
                catch
                {
                    //_xml.Append(" Attr1=\"" + "" + "\"");
                    //_xml.Append(" Attr2=\"" + "" + "\"");
                    //_xml.Append(" Attr3=\"" + ""+ "\"");
                    //_xml.Append(" Attr4=\"" + "" + "\"");
                    //_xml.Append(" Attr5=\"" + "" + "\"");
                    //_xml.Append(" Attr6=\"" + "" + "\"");
                    //_xml.Append(" Attr7=\"" + "" + "\"");
                    //_xml.Append(" Attr8=\"" + "" + "\"");
                }
                _xml.Append(" />");

                return _xml.ToString();
            }


        }

        public string XMLFinis
        {
            get
            {
                return "";
            }
        }
        public string XML
        {
            get
            {
                return XMLBoday + XMLFinis;
            }
        }
        #endregion
        #region AbstractConvertObject
        public override object ConvertToObject()
        {
            this.ID = IAM.Interface.Common.EnDeCoding.Decode(this.CurrentXmlNode.Attributes["ID"].InnerText);
            this.Display = IAM.Interface.Common.EnDeCoding.Decode(this.CurrentXmlNode.Attributes["Display"].InnerText);
            this.DimensionLevel = new DimensionLevel();
            this.DimensionLevel.ID = IAM.Interface.Common.EnDeCoding.Decode(this.CurrentXmlNode.Attributes["DimensionLevel"].InnerText);
            //if (this.Items != null)
            //{
            //    foreach (object obj in this.Items.Dimension.DimensionLevels.Items)
            //    {
            //        if (((DimensionLevel)obj).ID == this.DimensionLevel.ID)
            //        {
            //            this.DimensionLevel.Display = IAM.Interface.Common.EnDeCoding.Decode(((DimensionLevel)obj).Display);
            //            break;
            //        }
            //    }

            //}
            this.Parent = new ACItem();
            this.Parent.ID = IAM.Interface.Common.EnDeCoding.Decode(this.CurrentXmlNode.Attributes["Parent"].InnerText);
            //foreach (System.Object obj in this.Items.Items)
            //{
            //    if (((ACItem)obj).ID == this.Parent.ID)
            //    {
            //        this.Parent = (ACItem)obj;
            //        break;
            //    }
            //}
            try
            {
                this.Attr1 = IAM.Interface.Common.EnDeCoding.Decode(this.CurrentXmlNode.Attributes["Attr1"].InnerText);
                this.Attr2 = IAM.Interface.Common.EnDeCoding.Decode(this.CurrentXmlNode.Attributes["Attr2"].InnerText);
                this.Attr3 = IAM.Interface.Common.EnDeCoding.Decode(this.CurrentXmlNode.Attributes["Attr3"].InnerText);
                this.Attr4 = IAM.Interface.Common.EnDeCoding.Decode(this.CurrentXmlNode.Attributes["Attr4"].InnerText);
                this.Attr5 = IAM.Interface.Common.EnDeCoding.Decode(this.CurrentXmlNode.Attributes["Attr5"].InnerText);
                this.Attr6 = IAM.Interface.Common.EnDeCoding.Decode(this.CurrentXmlNode.Attributes["Attr6"].InnerText);
                this.Attr7 = IAM.Interface.Common.EnDeCoding.Decode(this.CurrentXmlNode.Attributes["Attr7"].InnerText);
                this.Attr8 = IAM.Interface.Common.EnDeCoding.Decode(this.CurrentXmlNode.Attributes["Attr8"].InnerText);
            }
            catch
            {
                
                //this.Attr1 = "";
                //this.Attr2 = "";
                //this.Attr3 = "";
                //this.Attr4 = "";
                //this.Attr5 = "";
                //this.Attr6 = "";
                //this.Attr7 = "";
                //this.Attr8 = "";
            }
            return this;
        }
        #endregion
        #region 重写Equals
        /// <summary>
        /// 确定指定的 ACItem 对象是否等效于当前 ACItem
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override bool Equals(object obj)
        {
            if (this == null && obj == null)
            {
                return true;
            }
            ACItem aci = null;
            try
            {
                aci = (ACItem)obj;
            }
            catch
            {
                return false;
            }
            bool result = true;
            if (this.ID != aci.ID)
            {
                result = false;
            }
            else if (this.Display != aci.Display)
            {
                result = false;
            }
            else if (this.DimensionLevel.ID!= aci.DimensionLevel.ID)
            {
                result = false;
            }
            else if (this.Parent.ID!=aci.Parent.ID)
            {
                result = false;
            }
            else if (this.Attr1 != aci.Attr1)
            {
                result = false;
            }
            else if (this.Attr2 != aci.Attr2)
            {
                result = false;
            }
            else if (this.Attr3 != aci.Attr3)
            {
                result = false;
            }
            else if (this.Attr4 != aci.Attr4)
            {
                result = false;
            }
            else if (this.Attr5 != aci.Attr5)
            {
                result = false;
            }
            else if (this.Attr6 != aci.Attr6)
            {
                result = false;
            }
            else if (this.Attr7 != aci.Attr7)
            {
                result = false;
            }
            else if (this.Attr8 != aci.Attr8)
            {
                result = false;
            }
            return result;
        }

        #endregion
        #region 重写GetHashCode
        /// <summary>
        /// 哈希函数
        /// </summary>
        /// <returns></returns>
        public override int GetHashCode()
        {
            int result = 1;
            PropertyInfo[] pi = this.GetType().GetProperties();
            foreach (PropertyInfo onofpi in pi)
            {
                result = result + onofpi.GetHashCode();
            }
            return result;
        }
        #endregion
    }
}
